define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");
	var globalInit = require("globalInit"); //共用js
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});
	var muiTips = require("muiTips"); //封装的弹框
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//返回清除缓存
	var dingTalkNav = require('dd_config');

	function getRightFun() {
		var popoverMore = $("header > a").attr("href");
		mui(popoverMore).popover('toggle');
	}
	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityWH_INPUT = authorityInit.getJurisdiction(authorityData, sourceType.WH_INPUT); //库存入库权限判断
	if(authorityWH_INPUT.indexOf(3) === -1) {
		muiTips.alertTips(' ', '您没有操作订单的权限，请联系管理员开通吧！', 2, 3, "default");
		$(".more").remove();
		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(false, "更多", "default");
	} else {
		$(".more").show(); //按钮显示
		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(true, "更多", getRightFun);
	}
	var eHistory = require("eHistory");

	var SourceId = sessionStorage.getItem('SourceId');
	var AuditId = sessionStorage.getItem('AuditId');
	var Audit_detailId = sessionStorage.getItem('Audit_detailId');
	var SourceType = sessionStorage.getItem('SourceType');
	var orderDetail;

	function historyAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, true, $('#notes').text());
	}

	function historyNoAudit() {
		eHistory.funAudit(AuditId, Audit_detailId, false, $('#notes').text());
	}
	mui('body').on('tap', '.mui-popover-action li>a', function() {
		var a = this,
			parent;
		//根据点击按钮，反推当前是哪个actionsheet
		for(parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
			if(parent.classList.contains('mui-popover-action')) {
				break;
			}
		}
		//关闭actionsheet
		mui('#' + parent.id).popover('toggle');
		switch($(this).text()) {
			case '重置草稿':
				muiTips.confirmTips(' ', '您确定要重置吗！', 2, 1, ResetDraft);
				break;
			case '通过':
				muiTips.alertTips(' ', '恭喜您已通过！', 1, 1, historyAudit);
				break;
			case '驳回':
				muiTips.alertTips(' ', '驳回成功！', 1, 1, historyNoAudit);
				break;
			case '编辑':
				sessionStorage.orderDetail = orderDetail;
				sessionStorage.changeOrderflag = "changeOrderflag";
				document.location.href = "../newInstock/newInstockOrder.html?dd_nav_bgcolor=FFFF943E";
				break;
			case '删除':
				muiTips.confirmTips(' ', '您确定要删除吗！', 2, 1, deleteOrder);
				break;
		}
	});

	var option = {};
	var dataModel = {};
	if(sessionStorage.getItem('SourceId') && sessionStorage.getItem('AuditId') && sessionStorage.getItem('Audit_detailId') && sessionStorage.getItem('SourceType')) {
		//审批中心入口
		dataModel = {
			"id": sessionStorage.getItem('SourceId')
		};
		$('header > a').attr('href', '#eForMore');
		//调用审批历史数据
		//钉钉返回清除缓存
		dingTalkNav.LeftNavBar(clearSessionSPDD);
		//ad
		dingTalkNav.leftAndroid(clearSessionSPDD);
		mui("body").on("tap", "header > button", function() {
			clearSessionSP();
		});
		setTimeout(function() {
			eHistory.historyCon('sHistoryList', '#historyList', SourceType, SourceId);
		}, 1000);
	} else {
		//钉钉返回清除缓存
		dingTalkNav.LeftNavBar(clearSessionDD);
		//ad
		dingTalkNav.leftAndroid(clearSessionDD);
		mui("body").on("tap", "header > button", function() {
			clearSession();
			document.location.href = "index.html?dd_nav_bgcolor=FFFF943E";
		});
		//单据页入口
		dataModel = {
			"id": sessionStorage.getItem('SourceId')
		};
	}
	abp.services.app.whInputList.getModel(dataModel, option).
	done(function(data) {

		orderDetail = JSON.stringify(data);

		if(data.statusId == abp.statusId.Draft ||
			data.statusId == abp.statusId.Auditing
		) {
			$(".more").attr("href", "#forMore2");
		} else {
			$(".more").attr("href", "#forMore");
		}

		//单据详情绑定
		$('#statusName').text(data.statusName);
		$('#serialNo').text(data.serialNo);
		$('#statusName').attr("data-statusId", data.statusId);
		$('#serialDate').text(data.serialDate.substring(0, 10));
		$('#companyName').text(data.companyName);
		$("#operatorName").text(data.operatorName);
		$('#totalAmount').text(data.totalAmount);
		$('#dataCreated').text(data.dataCreated.substring(0, 10));
		$('#notes').text(data.notes);

		//状态颜色
		var statusIdColor = require("colorOrder");
		statusIdColor("#statusName");

		//货品数据明细绑定
		var data1 = {};
		data1.list = data.whInputDetail;
		var html = template('inStockDetList', data1);
		$("#inStockDetUL").html(html);

		if(data.success) {

		} else {

		}
	}).fail(function(data) {

	});

	function deleteOrder() {
		var serialNo = $("#serialNo").text();
		var option = {};
		var dataModel = {
			"id": sessionStorage.SourceId,
			"serialNo": serialNo
		};
		abp.services.app.whInputList.delete(dataModel, option)
			.done(function(data) {
				document.location.href="../../src/inStock/index.html?dd_nav_bgcolor=FFFF943E";
			})
			.fail(function(data) {

			});
	}

	function ResetDraft() {
		var option = {};
		var dataModel = {
			"id": sessionStorage.SourceId
		};
		abp.services.app.whInputList.resetDraft(dataModel, option)
			.done(function(data) {
				clearSession();
				mui.back();

			})
			.fail(function(data) {
				muiTips.alertAutoma(' ', data.message, 2, "default");
			});
	}
	//返回清除缓存
	function clearSessionDD() {
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('SourceType');
		sessionStorage.removeItem('orderInDetail');
		sessionStorage.removeItem('orderInfo');
		sessionStorage.removeItem('outSourceType');
		sessionStorage.removeItem('sourceType');
		sessionStorage.removeItem('statusId');
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('changeOrderflag');
		document.location.href = "index.html?dd_nav_bgcolor=FFFF943E";
	}
	//返回清除缓存→审批
	function clearSessionSPDD() {
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('SourceType');
		sessionStorage.removeItem('changeOrderflag');
		document.location.href = "../Examination/index.html?dd_nav_bgcolor=FFFF943E";

	}

	//返回清除缓存
	function clearSession() {
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('SourceType');
		sessionStorage.removeItem('orderInDetail');
		sessionStorage.removeItem('orderInfo');
		sessionStorage.removeItem('outSourceType');
		sessionStorage.removeItem('sourceType');
		sessionStorage.removeItem('statusId');
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('changeOrderflag');
	}
	//返回清除缓存→审批
	function clearSessionSP() {
		sessionStorage.removeItem('SourceId');
		sessionStorage.removeItem('AuditId');
		sessionStorage.removeItem('Audit_detailId');
		sessionStorage.removeItem('SourceType');
		sessionStorage.removeItem('changeOrderflag');

	}
	globalInit.eHistoryInit("#historyList"); //审批历史数据提示
});